//
//  ViewController.swift
//  DanMuToSwift
//
//  Created by honeyrr on 2017/9/4.
//  Copyright © 2017年 MyMac. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var textField: UITextField!
    var label: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func send(_ sender: Any) {
        //主屏幕的宽高
        let WIDTH:CGFloat = self.view.frame.size.width;
        //减50 是减去Lable本身的高度
        let HEIGHT:CGFloat = self.view.frame.size.height - 50;
        // random: 在height中随机取一个数值
        let random:CGFloat = CGFloat(arc4random_uniform(UInt32(HEIGHT)))
        label = UILabel(frame: CGRect(x: WIDTH,y:random, width: 400, height: 50))
        self.view.addSubview(label)
        
        //判断文本输入框的内容是否为空
        if (textField.text?.isEmpty)! {
            print("输入的文本不能为空")
        } else {
            //获取输入的内容
            let getText:String = textField.text!
            //调用分类(分类很简单的,就是设置三原色)
            label.textColor = UIColor(red: CGFloat(arc4random_uniform(UInt32(255)))/255, green: CGFloat(arc4random_uniform(UInt32(255)))/255, blue: CGFloat(arc4random_uniform(UInt32(255)))/255, alpha: 1)
            //设置Lable的字体大小
            label.font = UIFont.systemFont(ofSize: 16)
            //将内容赋值给lable
            label.text = getText
            //移动文字
            moveText(label)
        }
        textField.text = nil
    }
    
    func moveText(_ label: UILabel) {
        //视图动画效果
        UIView.animate(withDuration: 10, animations: {
            //单独这一条语句无效果
            /*
             弹幕的原理是将Lable从最右侧移至最左侧,改变的是X的值,其他的值都不改变,所以从新设置Lable的X值就行了
             */
            label.frame = CGRect(x: 0 - label.frame.size.width, y: label.frame.origin.y, width: label.frame.size.width, height: label.frame.size.height)
        }) { (Bool) in
            //移动结束之后,从父视图中移除lable
            label.removeFromSuperview()
        }
    }
}

